<template>
  <div>
    <el-dialog
      class="base-dialog"
      :custom-class="'panna-dialog_big'"
      :title="title"
      width="1500px"
      align="left"
      :append-to-body="true"
      :visible.sync="innerDialogFormVisible"
      :before-close="handleClose"
    >
      <p slot="title">{{ title }}</p>

      <div class="menu-detail">
        <div class="menu-content">
          <el-table
            ref="table"
            :data="tableData"
            tooltip-effect="dark"
            fit
            border
            highlight-current-row
            style="width: 99%"
            @row-click="handleRowClick"
          >
            <el-table-column prop="sampleNum" label="样品编号" align="center" />
            <el-table-column
              prop="entrustTaskNum"
              label="任务编号"
              align="center"
            />
            <el-table-column
              prop="sampleName"
              label="样品名称"
              align="center"
            />
            <el-table-column
              prop="detectionItemName"
              label="检测项目"
              align="center"
            />
            <el-table-column
              prop="sampleStoreCount"
              label="样品数量"
              align="center"
            />
          </el-table>
        </div>
        <div v-if="rowIndex != -1" class="menu-operation">
          <el-form label-width="100px" status-icon>
            <el-descriptions class="margin-top" :column="1" :size="size" border>
              <el-descriptions-item>
                <template slot="label">
                  <span style="color: red">*</span>领取人
                </template>
                <el-form-item
                  :label-width="formLabelWidth"
                  prop="permissionUserId"
                >
                  <el-select
                    v-model="tableData[rowIndex].permissionUserId"
                    placeholder="请选择"
                    filterable
                    remote
                    reserve-keyword
                    :remote-method="remoteUser"
                  >
                    <el-option
                      v-for="item in userList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <span style="color: red">*</span>领取时间
                </template>
                <el-form-item
                  :label-width="formLabelWidth"
                  prop="sampleReceiveDate"
                >
                  <el-date-picker
                    v-model="tableData[rowIndex].sampleReceiveDate"
                    type="date"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-descriptions-item>

              <el-descriptions-item>
                <template slot="label">
                  <span style="color: red">*</span>领取数量
                </template>
                <el-form-item
                  :label-width="formLabelWidth"
                  prop="sampleReceiveCount"
                >
                  <el-input
                    v-model="tableData[rowIndex].sampleReceiveCount"
                    autocomplete="off"
                    oninput="value=value.replace(/[^\d]/g,'')"
                  ></el-input>
                </el-form-item>
              </el-descriptions-item>

              <el-descriptions-item>
                <template slot="label">
                  <span style="color: red">*</span>领取状态
                </template>
                <el-form-item :label-width="formLabelWidth" prop="receiveState">
                  <el-select
                    v-model="tableData[rowIndex].receiveState"
                    placeholder="请选择"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-descriptions-item>

              <el-descriptions-item>
                <template slot="label"> 领取备注 </template>
                <el-form-item
                  :label-width="formLabelWidth"
                  prop="receiveRemark"
                >
                  <el-input
                    v-model="tableData[rowIndex].receiveRemark"
                    type="textarea"
                  ></el-input>
                </el-form-item>
              </el-descriptions-item>
            </el-descriptions>
          </el-form>
        </div>
      </div>
      <span slot="footer" class="base-dialog-footer">
        <el-button type="primary" @click="onSubmit('formData')"
          >保 存</el-button
        >
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getUserListNoPage} from "@/ModulePermission/api/Permission/PermissionUser";
import { formatTime } from "../../../utils/time";
import { create } from "../../../api/SampleManage/SampleReservedSampleReceive.js";
import _ from "lodash";
export default {
  name: "SampleReceivingEdit",
  components: {},
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false,
    },
    rowData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      formatTime,
      submitReadOnly: true,
      rowIndex: -1,
      size: "",
      title: "留样领取",
      formLabelWidth: "0px",
      options: [
        { value: "10", label: "完好" },
        { value: "20", label: "已使用" },
        { value: "30", label: "破损" },
      ],
      tableData: [],
      userList: [],
    };
  },
  created() {
    this.innerDialogFormVisible = this.dialogFormVisible;
    this.init();
  },
  methods: {
    handleRowClick(row, column, event) {
      this.rowIndex = this.tableData.findIndex((item) => item.sysBusinessSampleInfoId === row.sysBusinessSampleInfoId);
    },
    // 提交
    onSubmit: _.throttle(function() {
      if (this.submitReadOnly) {
        this.submitReadOnly = false;
        for (let i = 0; i < this.tableData.length; i++) {
          if (
            this.tableData[i].permissionUserId === null ||
            this.tableData[i].permissionUserId === "" ||
            this.tableData[i].permissionUserId <= 0
          ) {
            this.$message.warning("请选择领取人");
            this.submitReadOnly = true;
            return false;
          }
          if (
            this.tableData[i].sampleReceiveDate === null ||
            this.tableData[i].sampleReceiveDate === "" ||
            this.tableData[i].sampleReceiveDate === undefined
          ) {
            this.$message.warning("请选择领取时间");
            this.submitReadOnly = true;
            return false;
          }

          if (
            this.tableData[i].sampleReceiveCount === null ||
            this.tableData[i].sampleReceiveCount === "" ||
            this.tableData[i].sampleReceiveCount === undefined
          ) {
            this.$message.warning("请填写领取数量");
            this.submitReadOnly = true;
            return false;
          }

          if (
            this.tableData[i].receiveState === null ||
            this.tableData[i].receiveState === "" ||
            this.tableData[i].receiveState === "0" ||
            this.tableData[i].receiveState === undefined
          ) {
            this.$message.warning("请选择领取状态");
            this.submitReadOnly = true;
            return false;
          }
        }
        this.addAPI();
        return false;
      }
    }, 2000),
    // 新增
    addAPI() {
      create(this.tableData)
        .then((res) => {
          this.submitReadOnly = true;
          this.handleClose();
        })
        .catch(() => {
          this.submitReadOnly = true;
        });
    },
    init() {
      var data = JSON.parse(JSON.stringify(this.rowData));

      for (let i = 0; i < data.length; i++) {
        data[i].permissionUserId = this.$store.getters.currentUserId;
        data[i].sampleReceiveDate = formatTime();
        data[i].sampleReceiveCount = data[i].sampleStoreCount;
        data[i].receiveRemark = "";
        data[i].receiveState = "10";
      }
      this.tableData = JSON.parse(JSON.stringify(data));

      getUserListNoPage({ userStatus: 1 })
        .then((res) => {
          this.userList = res.data;
        });
    },
    remoteUser(query) {
      getUserListNoPage({ name: query, userStatus: 1 })
        .then((res) => {
          this.userList = res.data;
        });
    },
    handleClose() {
      this.$emit("update:dialogFormVisible", false);
      this.$emit("closeSampleReceivingDialog");
      this.innerDialogFormVisible = false;
    },
  },
};
</script>
    <style lang="scss" >
.menu-detail {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 20px 0 20px 20px;
  overflow-y: auto;
  box-sizing: border-box;
  position: relative;
  display: flex;
  align-items: flex-start;
  .menu-content {
    width: 70%;
    min-height: 550px;
    border-right: 1px #dedede solid;
  }
  .empty {
    height: calc(100% - 40px);
  }
  .menu-operation {
    width: 30%;
    padding: 0 24px;
    box-sizing: border-box;
  }
}
</style>
